
package com.aistar.service.redis.impl;


import com.aistar.mapper.TravPraiseMapper;
import com.aistar.mapper.TravelnoteMapper;
import com.aistar.pojo.TravPraise;
import com.aistar.pojo.Travelnote;
import com.aistar.service.redis.RedisService;
import com.aistar.service.redis.TravPraiseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Service
public class TravPraiseServiceImpl implements TravPraiseService {
    @Autowired
    private TravPraiseMapper travPraiseMapper;
    @Autowired
    private RedisService redisService;
    @Autowired
    private TravelnoteMapper travelnoteMapper;
    /**
     * 保存点赞记录
     * @param
     * @return
     */
    @Override
    @Transactional
    public int save(TravPraise travPraise) {
        return travPraiseMapper.insert(travPraise);
    }

    /**
     * 批量保存或修改
     * @param list
     */
    @Override
    @Transactional
    public int saveAll(List<TravPraise> list) {
        Iterator<TravPraise> iterator = list.iterator();
        try{
            while (iterator.hasNext()){
                TravPraise travPraise = iterator.next();
                travPraise.setPraiseTime(new Date());
                travPraiseMapper.insert(travPraise);
            }
        }
        catch (Exception e){
            return -1;
        }
        return 1;
    }

    /**
     * 根据被点赞人的id查询点赞列表（即查询都谁给这个人点赞过）
     * @param likedUserId 被点赞人的id
     * @param pageable
     * @return
     */
    @Override
    public Page<TravPraise> getLikedListByLikedUserId(String likedUserId, Pageable pageable) {

        return null;
    }

    /**
     * 根据点赞人的id查询点赞列表（即查询这个人都给谁点赞过）
     * @param likedPostId
     * @param pageable
     * @return
     */
    @Override
    public Page<TravPraise> getLikedListByLikedPostId(String likedPostId, Pageable pageable) {
        return null;
    }

    /**
     * 通过被点赞人和点赞人id查询是否存在点赞记录
     * @param likedUserId
     * @param likedPostId
     * @return
     */
    @Override
    public TravPraise getByLikedUserIdAndLikedPostId(String likedUserId, String likedPostId) {
        return null;
    }

    /**
     * 将Redis里的点赞数据存入数据库中
     */
    @Override
    @Transactional
    public void transLikedFromRedis2DB() {
        List<TravPraise> list = redisService.getLikedDataFromRedis();
        System.out.println("list:"+list);
        Iterator<TravPraise> iterator = list.iterator();
        while (iterator.hasNext()){
            TravPraise travPraise = iterator.next();
            TravPraise ul = travPraiseMapper.selectByPrimaryKey(travPraise.getPraiseTravId(),travPraise.getPraiseUserId());
            System.out.println("找到了："+ul);
            if (ul == null){
                travPraise.setPraiseTime(new Date());
                //没有记录，直接存入
                System.out.println("存入结果："+save(travPraise));
            }else{
                System.out.println("ul:"+ul);
                //有记录，需要更新
                int status = travPraise.getPraiseStatus();
                ul.setPraiseStatus(status);
//                    ul.setCollectTime(ul.getCollectTime());
                ul.setPraiseUpdateTime(new Date());
                System.out.println(new Date());
                System.out.println("更新后的ul对象："+ul);
                int result = travPraiseMapper.updateByPrimaryKey(ul);
                System.out.println("更新结果："+result);
                System.out.println("跟新后的结果："+ul);
            }
        }
    }

    /**
     * 将Redis中的点赞数量数据存入数据库
     */
    @Override
    @Transactional
    public void transLikedCountFromRedis2DB() {
        //用list存放redis中对各篇游记的点赞数量（travnote对象，包含travid和点赞数）
        List<Travelnote> list = redisService.getLikedCountFromRedis();
        System.out.println("travelnotes:"+list);
        //用迭代器遍历list
        Iterator<Travelnote> iterator = list.iterator();
        while (iterator.hasNext()){
            Travelnote dto = iterator.next();
            Travelnote travelnote = travelnoteMapper.selectByPrimaryKey(dto.getTravId());
            //点赞数量属于无关紧要的操作，出错无需抛异常
            if (travelnote != null){

                int i =  travelnote.getTravPraise()+dto.getTravPraise();
                travelnote.setTravPraise(i);
                //更新点赞数量到travnote表
                int val = travelnoteMapper.updateByPrimaryKey(travelnote);
//                System.out.println("val:"+val);
//                System.out.println(travelnoteMapper.selectByPrimaryKey(travelnote.getTravId()));
        }

        }
    }
}
